package main

import (
	base "2024-fall-etohlcin3-backend/cmd/kitex_gen/industics/blockchain/base"
	"context"

	"github.com/hyperledger/fabric-gateway/pkg/client"
)

// ProductionServiceImpl implements the last service interface defined in the IDL.
type ProductionServiceImpl struct{}

var ProductionOrg *client.Contract

// GetLotTracing implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetLotTracing(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())

	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}

	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetWorkOrder implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetWorkOrder(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())

	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}

	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetESOP implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetESOP(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())

	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}

	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetLotTracingHistory implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetLotTracingHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetWorkOrderHistory implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetWorkOrderHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetESOPHistory implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetESOPHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetLotTracingByRange implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetLotTracingByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetWorkOrderByRange implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetWorkOrderByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetESOPByRange implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) GetESOPByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// CreateLotTracing implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) CreateLotTracing(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// CreateWorkOrder implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) CreateWorkOrder(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// CreateESOP implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) CreateESOP(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// UpdateLotTracing implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) UpdateLotTracing(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// UpdateWorkOrder implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) UpdateWorkOrder(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// UpdateESOP implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) UpdateESOP(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeleteLotTracing implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) DeleteLotTracing(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeleteWorkOrder implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) DeleteWorkOrder(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeleteESOP implements the ProductionServiceImpl interface.
func (s *ProductionServiceImpl) DeleteESOP(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

func readForKey(formID string) ([]byte, error) {
	return ProductionOrg.EvaluateTransaction("ReadForm", formID)
}
func readHistoryForKey(formID string) ([]byte, error) {
	return ProductionOrg.EvaluateTransaction("ReadFormHistoryForKey", formID)
}
func readForRange(formIDStart string, formIDEnd string) ([]byte, error) {
	return ProductionOrg.EvaluateTransaction("ReadFormsByRange", formIDStart, formIDEnd)
}
func createForKey(formID string, formJSON string) ([]byte, error) {
	return ProductionOrg.SubmitTransaction("CreateForm", formID, formJSON)
}
func updateForKey(formID string, formJSON string) ([]byte, error) {
	return ProductionOrg.SubmitTransaction("UpdateForm", formID, formJSON)
}
func deleteForKey(formID string) ([]byte, error) {
	return ProductionOrg.SubmitTransaction("DeleteForm", formID)
}
